import 'package:dali_ng_pera/global/dnp_global_manager.dart';
import 'package:dali_ng_pera/page/register/dnp_register_page.dart';
import 'package:dali_ng_pera/tool/dnp_location_helper.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class DnpMainController extends GetxController with GetSingleTickerProviderStateMixin {

  late BuildContext ctx;

  late TabController tabController;

  int selectMainIndex = 0;

  bool firstLoad = false;

  @override
  void onInit() {
    super.onInit();
    tabController = TabController(length: 3, vsync: this);
    tabController.addListener(_scrollToMainIndex);
  }

  void _scrollToMainIndex() {
    double currentIndex = tabController.index.toDouble();
    double animationIndex = tabController.animation!.value;

    if (currentIndex == animationIndex) {
      selectMainIndex = tabController.index;
      update();
    }
  }

  void clickMainIndex(int idx) {

    bool tabFlag = idx != 0;
    bool isRegister = DnpGlobalManager().isRegister;

    if (tabFlag && isRegister == false) {
      Navigator.of(ctx).push(MaterialPageRoute(builder: (_) => DnpRegisterPage()));
      return;
    }
    selectMainIndex = idx;
    tabController.index = idx;
    update();
  }

  void scrollToHomeWidget() {
    tabController.index = 0;
  }

  // void jumpApOrderPage(int index) {
  //   tabController.index = 1;
  //
  //   final controller = Get.put(APOrderController());
  //   controller.updateMenuIndex(index);
  // }
  void showAlertDialog(){
    if(!firstLoad){
      firstLoad = true;
      DnpLocationHelper.needPop;
    }
  }

}